package cn.schtwz.baselib.base.quickCode

import androidx.databinding.ViewDataBinding
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import cn.schtwz.baselib.R
import cn.schtwz.baselib.adapter.BaseDataBingdingQuickAdapter
import cn.schtwz.baselib.base.BaseBean
import cn.schtwz.baselib.base.BaseViewModelFragment
import cn.schtwz.baselib.databinding.FragmentBaseListBinding

import kotlinx.android.synthetic.main.activity_base_list.*
import kotlin.collections.HashMap

abstract class BaseListViewModelFragment<B : BaseBean, VB : ViewDataBinding> :
    BaseViewModelFragment<FragmentBaseListBinding>() {
    abstract var adapter: BaseDataBingdingQuickAdapter<B, VB>
    abstract override val viewModel: BaseListViewModel<B>
    abstract fun parameter(): HashMap<String, Any>//额外的参数
    override val layoutId: Int = R.layout.fragment_base_list


    override fun initView() {
        adapter.setOnLoadMoreListener {
            viewModel.loadDatas(parameter())
        }
        recyclerView.layoutManager = LinearLayoutManager(activity)
    }

    override fun initData() {
        viewModel.datas.observe(this, Observer {
            adapter.addPageData(it, viewModel.page.value!!)
        })

        viewModel.refreshing.observe(this, Observer {
            if (it) {
                viewModel.onRefresh(parameter())
            }

        })
        viewModel.refreshing.postValue(true)
    }


}